package com.itheima.reggie.conrtroller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 添加地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        //将当前线程中保存的用户账号与传入的地址关联
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 查询当前用户的全部地址
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        //将当前线程中保存的用户账号与传入的地址关联
        addressBook.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(addressBook.getUserId() != null, AddressBook::getUserId, addressBook.getUserId());
        wrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }


    /**
     * 根据id查找地址(回显用)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> findOne(@PathVariable Long id) {
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id != null, AddressBook::getId, id);
        AddressBook addressBook = addressBookService.getOne(wrapper);
        if (addressBook != null) {
            return R.success(addressBook);
        } else
            return R.error("没找到该对象");
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> SetDefault(@RequestBody AddressBook addressBook) {
        //获取当前账号的默认地址
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        wrapper.eq(AddressBook::getIsDefault,1);
        //将现有默认地址改为非默认地址(1->0)
        AddressBook addressBookServiceOne = addressBookService.getOne(wrapper);
        if (addressBookServiceOne!=null){
            addressBookServiceOne.setIsDefault(0);
            //修改数据库中的信息
            addressBookService.updateById(addressBookServiceOne);
        }
        //获取要修改为默认地址的对象，将它修改为默认地址(0->1)
        LambdaQueryWrapper<AddressBook> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(AddressBook::getId,addressBook.getId());
        AddressBook one = addressBookService.getOne(wrapper1);
        one.setIsDefault(1);
        //修改数据库中的信息
        addressBookService.updateById(one);
        return R.success(one);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook>getDefault(){
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        wrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(wrapper);
        if (addressBook==null){
            return R.error("没有找到该对象");
        }else {
            return R.success(addressBook);
        }
    }
}
